package main

import (
	"encoding/json"
	"fmt"
)

type Team struct {
	ID       int    `json:"id"`
	Name     string `json:"name"`
	Country  string `json:"country"`
	Founded  int    `json:"founded"`
	National bool   `json:"national"`
	Logo     string `json:"logo"`
}

func (t *Team) Marshal() (string, error) {
	data, err := json.Marshal(t)
	if err != nil {
		return "", err
	}
	return string(data), nil
}

func (t *Team) Unmarshal(data []byte) error {
	err := json.Unmarshal(data, t)
	if err != nil {
		return err
	}
	return nil
}

func main() {
	// 示例用法
	jsonData := `{
		"id": 33,
		"name": "Manchester United",
		"country": "England",
		"founded": 1878,
		"national": false,
		"logo": "https://media.api-sports.io/football/teams/33.png"
	}`

	// 将JSON数据解析为Team结构体
	team := Team{}
	err := team.Unmarshal([]byte(jsonData))
	if err != nil {
		fmt.Println("反序列化失败:", err)
		return
	}
	fmt.Println("反序列化后的team格式是:", team)

	// 将Team结构体转换为JSON字符串
	jsonStr, err := team.Marshal()
	if err != nil {
		fmt.Println("序列化为json格式失败:", err)
		return
	}
	fmt.Println("序列化为json格式为:", jsonStr)
}
